# frozen_string_literal: true

concern :CommonErrorPlugin do
  included do
    rescue_from CommonError::LoginError, with: :error_401
    rescue_from CommonError::AuthTokenError, with: :error_401
    rescue_from CommonError::CustomMessageError, with: :error_422
    rescue_from CommonError::ModelValidationError, with: :error_422
    rescue_from ActionController::ParameterMissing, with: :error_422
    # rescue_from RailsParam::Param::InvalidParameterError, with: :error_422
  end

  private

  def error_421(e)
    render json: { message: e.summary.split(':')[1] }.to_json, status: 421
  end

  def error_422(e)
    render json: { message: e.message }.to_json, status: 422
  end

  def error_401(e)
    render json: { message: e.message }.to_json, status: 401
  end
end
